/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neuronalenetzeelearning.view.mathoutput;

import atp.cHotEqn;
import java.awt.Dimension;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import neuronalenetzeelearning.model.Graph;
import neuronalenetzeelearning.model.Matrix;

/**
 *
 * @author svenmaster
 */
public class MathOutputComponent extends cHotEqn {


    public MathOutputComponent(Matrix matrix) {
        super();
        this.setEquation(getMatrixAsTexString(matrix));
    }

     public void updateMatrix(Matrix matrix) {
        this.setEquation(getMatrixAsTexString(matrix));
        //FIXME Calculate new width and assign it to the parent element when a new column is added to the matrix. Not working atm
        Dimension dimension = this.getSize();
        this.setPreferredSize(dimension);
        this.getParent().setSize((int)dimension.getWidth(), (int)this.getParent().getSize().getHeight());
        this.repaint();
    }

      public String getMatrixAsTexString(Matrix matrix){
        /* A Matrix in LaTeX looks like this (without the braces):
         * \begin{array}{ccc}
         * a & b & c \\
         * d & e & f \\
         * g & h & i
         * \end{array}
         */
        double m[][] = matrix.getValues();
        DecimalFormat df = new DecimalFormat("0.##");

        StringBuffer str = new StringBuffer("\\left[\\begin{array}{");
        for (int i = 0; i<m[0].length;i++){
            str.append("c");
        }
        str.append("}");

        for(int i = 0 ; i< m.length;i++){
            for (int j = 0; j<m[i].length; j++){
                str.append(df.format(m[i][j]));
                if (j<m[i].length-1){
                    str.append(" & ");
                }
            }
            str.append("\\\\");
        }

        str.append("\\end{array}\\right]");
        return str.toString();
    }

    public void printMatrixToFile(Matrix matrix, String path, int stepnumber, Vector<Double>inputvector, LinkedList<String>propertys){
        BufferedWriter file = null;
        if (path == null || path.equals("")){
            System.out.println("Fehler, Dateipfad ist leer!");
        }
        else{
            try {
                file = new BufferedWriter(new FileWriter(path, true));
            } catch (IOException ex) {
                System.out.println("Fehler, Datei nicht gefunden!");
            }
            try {
                if (file == null){
                    System.out.println("Fehler");
                }
                else{
                    if (stepnumber == 0){
                        for (int i = 0; i < propertys.size(); i++){
                            file.write(propertys.get(i));
                            file.newLine();
                        }
                        file.newLine();
                    }
                    file.write("Stepnumber: "+stepnumber+"   Eingabevektor"+inputvector.toString());
                    file.newLine();
                    for (int i = 0; i < matrix.getValues().length; i++){
                        for (int j = 0; j < matrix.getValues()[0].length; j++){
                            DecimalFormat df = new DecimalFormat("#.00");
                            file.write(df.format(matrix.getValues()[i][j]));
                            file.write("\t");
                        }
                        file.newLine();
                    }
                    file.newLine();
                    file.close();
                }
            }
            catch (IOException ex) {
               System.out.println("Fehler beim schreiben der Datei");
            }
        }
    }

    public void printMatrixToFile(Matrix matrix, String path, int stepnumber, LinkedList<String>propertys){
        BufferedWriter file = null;
        if (path == null || path.equals("")){
            System.out.println("Fehler, Dateipfad ist leer!");
        }
        else{
            try {
                file = new BufferedWriter(new FileWriter(path, true));
            } catch (IOException ex) {
                System.out.println("Fehler, Datei nicht gefunden!");
            }
            try {
                if (file == null){
                    System.out.println("Fehler");
                }
                else{
                    if (stepnumber == 0){
                        for (int i = 0; i < propertys.size(); i++){
                            file.write(propertys.get(i));
                            file.newLine();
                        }
                        file.newLine();
                    }
                    file.write("Ausgangsmatrix:");
                    file.newLine();
                    for (int i = 0; i < matrix.getValues().length; i++){
                        for (int j = 0; j < matrix.getValues()[0].length; j++){
                            DecimalFormat df = new DecimalFormat("#.00");
                            file.write(df.format(matrix.getValues()[i][j]));
                            file.write("\t");
                        }
                        file.newLine();
                    }
                    file.newLine();
                    file.close();
                }
            }
            catch (IOException ex) {
               System.out.println("Fehler beim schreiben der Datei");
            }
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MathOutputComponent(Graph.getExampleGraph().getMatrix()));
                frame.pack();
                frame.setVisible(true);
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
